package com.smarttiger.zero.admin.service;

import com.smarttiger.zero.admin.dao.InterfaceDao;
import com.smarttiger.zero.admin.dao.InterfaceLogicDao;
import com.smarttiger.zero.admin.dto.InterfaceLogicDto;
import com.smarttiger.zero.admin.entity.InterfaceLogicModel;
import com.smarttiger.zero.admin.entity.InterfaceModel;
import com.smarttiger.zero.admin.util.ParamNames;
import com.smarttiger.zero.admin.webservice.WebServiceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class InterfaceLogicService extends BaseService<InterfaceLogicModel> {
    @Autowired
    InterfaceLogicDao interfaceLogicDao;

    @Autowired
    WebServiceConfig webServiceConfig;

    @Autowired
    InterfaceDao interfaceDao;

    @PostConstruct
    public void initMethod() {
        super.setDao(interfaceLogicDao);
    }

    @ParamNames("interfaceId")
    public List<InterfaceLogicDto> findLogicByInterfaceId(String id) {
        return interfaceLogicDao.findAllByInterfaceId2(id);
    }

    @Override
    public void save(InterfaceLogicModel u) {
        InterfaceModel in = interfaceDao.findById(u.getInterfaceId()).get();
        webServiceConfig.stopWS(in);
        super.save(u);
    }


    @Override
    public void delete(String id) {
        InterfaceLogicModel u = findById(id).get(0);
        InterfaceModel in = interfaceDao.findById(u.getInterfaceId()).get();
        webServiceConfig.stopWS(in);
        super.delete(id);
    }
}
